package com.dz.goods.service;

import com.dz.common.base.BasePageResultsVO;
import com.dz.common.constants.CodeDomain;
import com.dz.goods.domain.entity.Brand;
import com.dz.goods.domain.entity.QBrand;
import com.dz.goods.domain.entity.QCategory;
import com.dz.goods.domain.entity.QCategoryBrand;
import com.dz.goods.domain.repository.BrandRepository;
import com.dz.goods.domain.vo.BrandParamsVO;
import com.dz.goods.domain.vo.BrandResultsVO;
import com.querydsl.core.BooleanBuilder;
import com.querydsl.core.QueryResults;
import com.querydsl.jpa.impl.JPAQuery;
import com.querydsl.jpa.impl.JPAQueryFactory;
import org.apache.commons.lang3.StringUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.transaction.annotation.Transactional;

import java.util.List;
import java.util.Optional;
import java.util.stream.Collectors;

@Service
public class BrandServiceImpl implements BrandService{

    @Autowired
    private BrandRepository brandRepository;
    @Autowired
    private JPAQueryFactory jpaQueryFactory;

    /**
     * 全部数据
     * @return
     */
    @Transactional
    @Override
    public List<Brand> findAll(){
        return brandRepository.findAll();
    }
    @Transactional
    @Override
    public Brand findById(Long id) {
        Optional<Brand> brand = brandRepository.findById(id);
        return brand.get();
    }
    @Transactional
    @Override
    public Brand add(Brand brand) {
        return brandRepository.saveAndFlush(brand);
    }
    @Transactional
    @Override
    public void delete(Long id) {
        Brand brand = brandRepository.findById(id).get();
        brand.setDeleteFlag(Integer.valueOf(CodeDomain.DELETE_FLAG_YES.value()));
        brandRepository.saveAndFlush(brand);
    }
    @Transactional
    @Override
    public Brand edit(Brand brand) {
        return brandRepository.saveAndFlush(brand);
    }
    @Transactional
    @Override
    public BasePageResultsVO<BrandResultsVO> getBrandList(BrandParamsVO vo) {
        QBrand brand= QBrand.brand;
        BooleanBuilder where=new BooleanBuilder();
        if(StringUtils.isNotEmpty(vo.getId())){
            where.and(brand.id.eq(Long.parseLong(vo.getId())));
        }
        if(StringUtils.isNotEmpty(vo.getName())){
            where.and(brand.name.eq(vo.getName()));
        }
        if(StringUtils.isNotEmpty(vo.getLetter())){
            where.and(brand.letter.eq(vo.getLetter()));
        }
        where.and(brand.deleteFlag.eq(Integer.valueOf(CodeDomain.DELETE_FLAG_NO.value())));
        JPAQuery<Brand> query=jpaQueryFactory.select(brand).from(brand).where(where.getValue());
        QueryResults<Brand> fetchResults = query.offset((vo.getPageNum() - 1L) * vo.getPageSize()).limit(vo.getPageSize()).fetchResults();
        List<BrandResultsVO> list=fetchResults.getResults().stream().map(e->BrandResultsVO.builder().id(e.getId())
        .brandName(e.getName())
        .brandLogo(e.getBrandLogo())
        .letter(e.getLetter())
        .build()).collect(Collectors.toList());
        return BasePageResultsVO.success(vo.getPageNum(),vo.getPageSize(),fetchResults.getTotal(),list);
    }
    @Transactional
    @Override
    public List<BrandResultsVO> findByCategory(Long categoryid) {
        QBrand brand= QBrand.brand;
        QCategoryBrand categoryBrand= QCategoryBrand.categoryBrand;
        List<Brand> brandList = jpaQueryFactory.select(brand).from(brand).innerJoin(categoryBrand)
                .on(brand.id.eq(categoryBrand.brandId))
                .where(categoryBrand.categoryId.eq(categoryid)).fetch();
        List<BrandResultsVO> resultsVOList = brandList.stream().map(e -> BrandResultsVO.builder().id(e.getId())
                .brandName(e.getName())
                .brandLogo(e.getBrandLogo())
                .letter(e.getLetter()).build()).collect(Collectors.toList());

        return resultsVOList;
    }
}